package com.yq.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.reggie.controller.Result;
import com.yq.reggie.dto.DishDto;
import com.yq.reggie.mapper.DishMapper;
import com.yq.reggie.pojo.Dish;
import com.yq.reggie.pojo.DishFlavor;
import com.yq.reggie.service.DishFlavorService;
import com.yq.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @Override
    @Transactional
    public Result addWithDishFlavor(DishDto dishDto) {
        Dish dish = new Dish();
        List<DishFlavor> flavors = dishDto.getFlavors();
        BeanUtils.copyProperties(dishDto,dish);

        //添加菜品
        this.save(dish);
        //添加菜品口味
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
            dishFlavorService.save(flavor);
        }
        return Result.success("添加菜品成功");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result deleteWithDishFlavor(Long[] ids) {
        for (Long id : ids) {
            //删除菜品
            this.removeById(id);
            //删除菜品口味
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(queryWrapper);
        }
        return Result.success("删除菜品成功");
    }

    /**
     * 修改菜品：页面回显
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result modify_displayWithDisFlavor(Long id) {
        //查询菜品
        Dish dish = this.getById(id);
        //查询菜品口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavors);
        return Result.success(dishDto);
    }

    /**
     *  修改菜品
     * @param dishDto
     * @return
     */
    @Transactional
    @Override
    public Result modifyWithDishFlavor(DishDto dishDto) {
        List<DishFlavor> flavors = dishDto.getFlavors();
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        //修改菜品
        this.updateById(dish);
        //修改菜品口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishFlavorService.removeBatchByIds(list);
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
            dishFlavorService.save(flavor);
        }
        return Result.success("修改成功");
    }

}
